package net.lainiao;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
import org.apache.curator.framework.recipes.locks.Lease;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/11/10.
 */
public class InterProcessSemaphoreExample {
    public static final int MAX_LEASE=10;
    public static final String PATH="/InterProcessSemaphoreExample/locks";

    public static void main(String[] args) throws Exception {
        FakeLimitedResource resource=new FakeLimitedResource();
        try (TestingServer server=new TestingServer()){
            CuratorFramework client= CuratorFrameworkFactory.newClient(server.getConnectString(),new ExponentialBackoffRetry(1000,3));
            client.start();
            InterProcessSemaphoreV2 semaphoreV2=new InterProcessSemaphoreV2(client,PATH,MAX_LEASE);
            Collection<Lease> leases=semaphoreV2.acquire(5);
            System.out.println("get"+leases.size()+" leases");
            Lease lease=semaphoreV2.acquire();
            System.out.println("get another lease");
            resource.use();
            Collection<Lease> leases2=semaphoreV2.acquire(5,10, TimeUnit.SECONDS);
            System.out.println("Should timeout and acquire return "+leases2);
            System.out.println("return one lease");
            semaphoreV2.returnLease(lease);
            System.out.println("return another 5 release ");
            semaphoreV2.returnAll(leases);
        }
    }
}
